package com.chenjt.netty.ten_zerocopy;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/9/2 11:07
 */
public class NewIOClient {

    public static void main(String[] args) throws Exception{

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost",8899));
        socketChannel.configureBlocking(true);

        // 找一个几百兆的文件的绝对路径
        String fileName = "";

        FileChannel fileChannel = new FileInputStream(fileName).getChannel();

        long startTime = System.currentTimeMillis();

        // transferTo 用到了操作系统零拷贝的特性
        long transferCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);

        System.out.println("发送总字节数：" + transferCount + "，耗时：" + (System.currentTimeMillis() - startTime));

        fileChannel.close();

    }

}
